//
//  NetworkTool.m
//  网易新闻23
//
//  Created by apple on 16/12/27.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "NetworkTool.h"

@implementation NetworkTool

+ (instancetype)sharedTool {
    
    // 不能用id,如果是id就不能调出父类的内容
    static NetworkTool *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
        
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
        // 让AFN返回原始的二进制数据,我们自己反序列化
        instance.responseSerializer = [AFHTTPResponseSerializer serializer];
        // 修改AFN默认的发送数据的类型,改为JOSN
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
    });
    
    return instance;
}

- (void)POSTWith:(NSString *)urlStr parameters:(id)parameters success:(void (^)(id))successBlock failed:(void (^)(NSError *))failedBlock {
    
    //    NSLog(@"%@",urlStr);
    [self POST:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 把AFN获取到的responseObject,交给tool,回调给外界
        if (successBlock) {
            successBlock(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 把AFN获取的错误信息,交给tool,回调给外界
        if (failedBlock) {
            failedBlock(error);
        }
    }];
    
}


@end
